package cn.bellychang.recursion.bobo.cl02AddFirst;

import java.util.Objects;

/**
 * Utils class using an abstract modifier indicates this class cannot be extended by a subclass
 * @author: bellychang
 * @date: 2020/2/15
 */
public abstract class ArrayUtils {

    public static int sumByRecursion(int[] array) {
        if (Objects.isNull(array)) {
            throw new IllegalArgumentException();
        }
        return sumArrayByLength(array, 0);
    }

    /**
     * sum [start ... array.length)
     * @param array
     * @param start start position
     * @return
     */
    private static int sumArrayByLength(int[] array, int start) {
        if (start == array.length) {
            // solve the most basic question
            return 0;
        } else {
            // convert original question to a smaller one
            return sumArrayByLength(array, start + 1) + array[start];
        }
    }
}
